from flask import Flask, render_template, redirect, views

app = Flask(__name__)
import functools


def wapper(func):
    """装饰器"""

    @functools.wraps(func)
    def inner(*args, **kwargs):
        print('before')
        return func(*args, **kwargs)

    return inner


# FBV
@app.route('/xxxx', methods=['GET', 'POST'])
@wapper
def index():
    return "Index"


# CBV
# 方式一
"""
class IndexView(views.View):
    methods = ['GET']
    decorators = [wapper, ]

    def dispatch_request(self):
        print('Index')
        return 'Index!'

app.add_url_rule('/index', view_func=IndexView.as_view(name='index1'))  # name=endpoint，这里的name就和route(endpoint反向生成一样)
"""


# 方式二
class IndexView(views.MethodView):
    methods = ['GET', 'POST']  # 支持方法
    decorators = [wapper, ]

    def get(self):
        return 'Index.GET'

    def post(self):
        return 'Index.POST'


app.add_url_rule('/index', view_func=IndexView.as_view(name='index2'))  # name=endpoint

if __name__ == '__main__':
    app.run()
